package com.tianqu.tms.www.car.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.MarketCarGateInfo;
import com.tianqu.tms.www.car.actionform.CarImportExportGateActionForm;
import com.tianqu.tms.www.car.viewhelper.CarImportExportGateViewHelper;

public class CarImportExportMonitoringGateListCotroller extends TmsController {
	private Logger logger = Logger.getLogger(CarImportExportMonitoringGateListCotroller.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			CarImportExportGateViewHelper viewhelper = (CarImportExportGateViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			CarImportExportGateActionForm form = (CarImportExportGateActionForm) uc.getInputForm();
			String updateFlag = (String)uc.getAttribute("updateFlag");
			Date date = new Date();//当前日期
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化对象
			MarketCarGateInfo gateInfo = new MarketCarGateInfo();
			Codes code = Codes.getInstance();
			if("1".equals(updateFlag)){
				String gateId1 = (String)uc.getAttribute("gateId1");
				String gateId2 = (String)uc.getAttribute("gateId2");
				List<MarketCarGateInfo> gateInfoList1 = new ArrayList<MarketCarGateInfo>();
				List<MarketCarGateInfo> gateInfoList2 = new ArrayList<MarketCarGateInfo>();
				if(!StringUtil.isNullOrBlank(gateId1)&&!"0".equals(gateId1)){
					gateInfoList1 = gateInfo.findGateInfoByGateId(Integer.valueOf(gateId1),sdf.format(date));
					if(gateInfoList1.size()>0){
						viewhelper.setInsertTimes1(StringUtil.getTimeByTimeStamps(gateInfoList1.get(0).getInsertTime()));
						viewhelper.setGateInfoListA(setCarGateInfo(gateInfoList1));				
					}
				}
				if(!StringUtil.isNullOrBlank(gateId2)&&!"0".equals(gateId2)){
					gateInfoList2 = gateInfo.findGateInfoByGateId(Integer.valueOf(gateId2),sdf.format(date));
					if(gateInfoList2.size()>0){
						viewhelper.setGateInfoListB(setCarGateInfo(gateInfoList2));
						viewhelper.setInsertTimes2(StringUtil.getTimeByTimeStamps(gateInfoList2.get(0).getInsertTime()));
					}
				}
				
			}else{
				String gateId = (String)uc.getAttribute("gateId");
				String gateIp = "";
				if(!"0".equals(gateId)&&!StringUtil.isNullOrBlank(gateId)){
					gateIp = code.getItemValueByItemIdAndItemCode(Codes.GATEWAY_IP, Integer.valueOf(gateId));
				}
				String entranceType = (String)uc.getParameter("entranceType");
				List<MarketCarGateInfo> gateInfoList = new ArrayList<MarketCarGateInfo>();
				gateInfoList = gateInfo.findGateInfoByGateId(Integer.valueOf(gateId),sdf.format(date));
				//更新时间
				String insertTime = "";
				if(gateInfoList.size()>0){
					insertTime = StringUtil.getTimeByTimeStamps(gateInfoList.get(0).getInsertTime());
					gateInfoList = setCarGateInfo(gateInfoList);			
				}
				if("1".equals(entranceType)){
					viewhelper.setGateId1(form.getGateId());
					viewhelper.setGateInfoListA(gateInfoList);
					viewhelper.setInsertTimes1(insertTime);
					viewhelper.setGateIP1(gateIp);
				}else if("2".equals(entranceType)){
					viewhelper.setGateId2(form.getGateId());
					viewhelper.setGateInfoListB(gateInfoList);
					viewhelper.setInsertTimes2(insertTime);
					viewhelper.setGateIP2(gateIp);
				}
			}
			viewhelper.setNowDate(sdf.format(date));
			uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);

			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
	private List<MarketCarGateInfo> setCarGateInfo(List<MarketCarGateInfo> list) throws TmsDaoException{
		//易碎贴
		String tempLicensePlate = "";
		//识别状态
		String threadStatusInfo = "";
		//商户名称
		String shortName = "";
		//识别方式
		String threadTypeInfo = "";
		Car car = new Car();
		for(MarketCarGateInfo info:list){
			if(info != null){
				Codes code = Codes.getInstance();
				SimpleDateFormat fm = new SimpleDateFormat("HH:mm");
				//进出时间
				if(info.getInsertTime() != null) info.setInsertTimes(fm.format(info.getInsertTime()));
				//商户名称
				shortName = car.findShortNameBySerial(info.getSerial());						
				info.setMemberName(shortName);
				//易碎贴
				tempLicensePlate = info.getTempLicensePlate();
				if(!StringUtil.isNullOrBlank(tempLicensePlate)){
					tempLicensePlate = tempLicensePlate.substring(tempLicensePlate.length() - 8, tempLicensePlate.length());
				}
				info.setTempLicensePlate(tempLicensePlate);								
				//进出状态
				threadStatusInfo = code.getItemValueByItemIdAndItemCode(Codes.THREADSTATUS_TYPE, info.getThreadStatus());
				info.setThreadStatusInfo(threadStatusInfo);
				//识别方式
				threadTypeInfo = code.getItemValueByItemIdAndItemCode(Codes.THREADTYPE_TYPE, info.getThreadType());
				info.setThreadTypeInfo(threadTypeInfo);				
				//过滤方式
				if(info.getCheckFlag() == 1){
					info.setCheckFlags("off");
				}else {
					info.setCheckFlags("on");
				}
			}
		}
		return list;
	}
}
